<template>
	<sl-page title="我的顾客">
		<view class="customer">
			<view class="customer-tab">
				<view class="customer-tab-menu" :class="{ active: item.active}" v-for="(item, index) in customerAnalyse"
					:key="index" @click="handleCustomerTabResult(item)">
					<view class="py-1">{{ item.name }}</view>
					<view class="py-1 f-w f-36">{{ item.count }}</view>
				</view>
			</view>
			<z-paging ref="paging" :paging-style="{'margin-top':top + 'px'}" v-model="customerList" @query="loadPage">
				<view class="customer-page">
					<List :listDatas="customerList">
						<template v-slot="{ item }">
							<view class="item">
								<!-- 订单状态和时间 -->
								<view class="item-state">
									<view class="flex flex-y-center">
										<view class="profile-headimg">
											<sl-image :value="item.vipImage"></sl-image>
										</view>
										<view class="f-w">{{ item.userName }}</view>
									</view>
									<view class="state-level">
										{{ item.vipName }}
									</view>
								</view>
								<view class="item-body">
									<view class="f-24 f-tip">
										首次点击邀请时间：{{ item.beginTime }}
									</view>
									<view class="f-24 f-tip py-1">
										查看商品次数：{{ item.vipTime }}
									</view>
									<view class="f-24 f-tip">
										首次下单时间：2023-11-11 11:30:30
									</view>
								</view>
							</view>
						</template>
					</List>
				</view>
			</z-paging>
		</view>
	</sl-page>
</template>

<script>
	import DistributionApi from '@/api/distribution/distribution.js';
	import Constants from '@/constants';
	import List from '@/pages/components/list/list.vue';
	//
	export default {
		//
		name: 'customerIndex',
		//
		mixins: [Constants],
		//
		components: {
			List,
		},
		//
		data() {
			return {
				//
				options: {},
				//
				query: {},
				//是否选中
				isActive: true,
				count: '0',
				perpetualCount: '0',
				customers: [],
				perpetualCustomers: [],
				//
				customerList: [],
				top: '',
				//顾客分析
				customerAnalyse: [{
						id: '1',
						name: '20日顾客',
						count: '0',
						active: true,
					},
					{
						id: '2',
						name: '永久顾客',
						count: '0',
						active: false,
					}
				],
			};
		},
		//
		onLoad() {},
		//
		onShow() {
			//
			this.$nextTick(() => {
				const query = uni.createSelectorQuery().in(this);
				query.select('.customer-tab').boundingClientRect(data => {
					this.top = this.CustomBar + data.height;
				}).exec();
			});
		},
		//
		methods: {
			//初始化20日，永久顾客的数据
			initCustomerMenu() {
				this.customerAnalyse[0].count = this.count;
				this.customerAnalyse[1].count = this.perpetualCount;
			},
			//tab切换查询参数
			handleCustomerTabResult(item) {
				if (item.id === '1') {
					this.customerAnalyse[0].active = true;
					this.customerAnalyse[1].active = false;
					this.$refs.paging.complete(this.customers);
				} else if (item.id === '2') {
					this.customerAnalyse[0].active = false;
					this.customerAnalyse[1].active = true;
					this.$refs.paging.complete(this.perpetualCustomers);
				}
			},
			//加载我的顾客
			loadPage: async function() {
				try {
					const res = await DistributionApi.customers(this.query, this.param);
					const perpetualRes = await DistributionApi.perpetualCustomers(this.query, this.param);
					//
					this.count = res.obj.param.recordCount;
					this.perpetualCount = perpetualRes.obj.param.recordCount;
					//
					this.customers = res.obj.data;
					this.perpetualCustomers = perpetualRes.obj.data;
					//
					this.initCustomerMenu();
					this.$refs.paging.complete(this.customers);
				} catch (error) {
					console.error('【customerIndex:loadPage】:', error);
					this.$refs.paging.complete(false);
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	.customer {
		font-size: $uni-font-size-base;
		color: $uni-text-color-primary;
		padding: 16rpx 32rpx;

		.customer-tab {
			display: flex;
			justify-content: space-around;
			padding: 12rpx 0rpx;
			height: 76px;
			flex-shrink: 0;
			border-radius: 8px;
			background: #FFF;
			box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.06);

			.customer-tab-menu {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 95px;
				height: 64px;
				flex-shrink: 0;
				border-radius: 8px;
			}
		}

		.customer-page {
			margin-top: 24rpx;
			margin-left: 32rpx;
			margin-right: 32rpx;

			.item {
				.item-state {
					display: flex;
					justify-content: space-between;
					padding-top: 24rpx;

					.profile-headimg {
						position: relative;
						width: 52rpx;
						height: 52rpx;
						margin-right: 20rpx;
						overflow: hidden;
						border-radius: 8rpx;
					}

					.state-level {
						color: #CB6E00;
						display: inline-flex;
						padding: 0px 6px;
						align-items: center;
						justify-content: center;
						gap: 10px;
						border-radius: 14px;
						background: linear-gradient(315deg, #FFF8B6 0%, #FCE2BC 100%);

					}
				}

				.item-body {
					height: 76px;
					flex-shrink: 0;
					background: #F7F7F7;
					margin-top: 24rpx;
					padding: 20rpx;
				}
			}
		}
	}

	.active {
		border: 1px solid #F9E6DE;
		background: linear-gradient(180deg, #FFEDE8 0%, rgba(243, 243, 243, 0.00) 100%);
	}
</style>